/*
 * AccountTableModel.java
 *
 *  
 */
package org.msb.finance.models;

import java.math.BigDecimal;

import javax.swing.table.AbstractTableModel;

import org.msb.finance.app.FinanceApp;
import org.msb.finance.data.Account;

/**
 * 
 * 
 * @author Marc Boudreau
 * 
 */
@SuppressWarnings("serial")
public class AccountTableModel extends AbstractTableModel {

	/*
	 * 
	 */
	private final String[] COLUMN_NAMES = { "Name", "Balance" };

	/*
	 * 
	 */
	private final Class<?>[] COLUMN_CLASSES = { String.class, BigDecimal.class };

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
	 */
	@Override
	public Class<?> getColumnClass(int col) {
		assert col >= 0 && col < this.COLUMN_CLASSES.length;

		return this.COLUMN_CLASSES[col];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return this.COLUMN_CLASSES.length;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	public int getRowCount() {
		return null == FinanceApp.getCabinet() ? 0 : FinanceApp.getCabinet().getAccounts().size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	public Object getValueAt(int row, int col) {
		assert row >= 0 && row < FinanceApp.getCabinet().getAccounts().size();
		assert col >= 0 && col < this.COLUMN_CLASSES.length : "Invalid column index specified to AccountTableModel.getValueAt()"; //$NON-NLS-1$

		Account account = FinanceApp.getCabinet().getAccounts().get(row);

		switch (col) {
			case 0:
				return account.getName();
			case 1:
				return account.getBalance();
		}

		return null;
	}

	/**
	 * 
	 * @param account
	 */
	public void addAccount(Account account) {
		FinanceApp.getCabinet().addAccount(account);

		fireTableDataChanged();
	}

	/**
	 * 
	 * @param index
	 * @return
	 */
	public Account getAccountAt(int index) {
		return FinanceApp.getCabinet().getAccounts().get(index);
	}
}
